package controllers

import (
	"context"
	"errors"
	"github.com/gin-gonic/gin"
	"go.didapinche.com/foundation/plat-echo/app/ops/models"
	"go.didapinche.com/foundation/plat-echo/app/ops/services"
	"go.didapinche.com/foundation/plat-echo/pkg/constants"
	"go.uber.org/zap"
	"net/http"
	"strconv"
)

type TempTypeController struct {
	service services.TempTypeService
	logger  *zap.Logger
}

func NewTempTypeController(service services.TempTypeService, logger *zap.Logger) *TempTypeController {
	return &TempTypeController{
		service: service,
		logger:  logger.With(zap.String("type", "controller.temptype")),
	}
}

func (ctl *TempTypeController) FindAll(c *gin.Context) {
	//默认0是应用 1是api_http 2是zeus
	templateType, _ := strconv.ParseInt(c.DefaultQuery("template_type", "0"), 10, 64)

	types, err := ctl.service.FindByTemplateType(c.Request.Context(), models.TypeEnum(templateType))
	if err != nil {
		if errors.Is(err, context.Canceled) {
			ctl.logger.Error("context canceled", zap.Error(err))
			c.Status(constants.StatusRequestCanceled)
			return
		}
		ctl.logger.Error("call service FindByTemplateType error", zap.Error(err))
		c.String(http.StatusInternalServerError, err.Error())
		return
	}
	c.JSON(http.StatusOK, types)
}

func (ctl *TempTypeController) Create(c *gin.Context) {
	req := new(models.TempType)
	if err := c.BindJSON(req); err != nil {
		ctl.logger.Error("bind params error", zap.Error(err))
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	tempType, err := ctl.service.Create(c.Request.Context(), req)
	if err != nil {
		if errors.Is(err, context.Canceled) {
			ctl.logger.Error("context canceled", zap.Error(err))
			c.Status(constants.StatusRequestCanceled)
			return
		}
		ctl.logger.Error("call service Create error", zap.Error(err))
		c.String(http.StatusNotImplemented, err.Error())
		return
	}
	c.JSON(http.StatusOK, tempType)
}

func (ctl *TempTypeController) Update(c *gin.Context) {
	req := new(models.TempType)
	if err := c.BindJSON(req); err != nil {
		ctl.logger.Error("bind params error", zap.Error(err))
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	tempType, err := ctl.service.Update(c.Request.Context(), req)
	if err != nil {
		if errors.Is(err, context.Canceled) {
			ctl.logger.Error("context canceled", zap.Error(err))
			c.Status(constants.StatusRequestCanceled)
			return
		}
		ctl.logger.Error("call service Update error", zap.Error(err))
		c.String(http.StatusNotImplemented, err.Error())
		return
	}
	c.JSON(http.StatusOK, tempType)
}

func (ctl *TempTypeController) Delete(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		ctl.logger.Error("parse id err", zap.Error(err))
		c.String(http.StatusBadRequest, "parse id err:%v", err)
		return
	}
	err = ctl.service.Delete(c.Request.Context(), id)
	if err != nil {
		if errors.Is(err, context.Canceled) {
			ctl.logger.Error("context canceled", zap.Error(err))
			c.Status(constants.StatusRequestCanceled)
			return
		}
		ctl.logger.Error("call service Delete error", zap.Error(err))
		c.String(http.StatusNotImplemented, err.Error())
		return
	}
	c.JSON(http.StatusOK, "ok")
}
